﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EditMapTank : MonoBehaviour
{
    //属性值
    private float moveSleep = 3f;
    private float createCD = 0;
    private bool isClear;


    // 引用变量

    //精灵渲染器
    private SpriteRenderer spriteRenderer;

    // 墙壁引用
    public GameObject Wall;
    //障碍引用
    public GameObject Barriar;
    //草引用
    public GameObject Grass;
    //水引用
    public GameObject River;
    public GameObject edge;


    private void Awake()
    {
        //初始化渲染器
        spriteRenderer = GetComponent<SpriteRenderer>();
    }


    // Start is called before the first frame update
    void Start()
    {
        // x轴
        for (int i = -10; i <= 10; i++)
        {
            Instantiate(edge, new Vector3(i, 9, 0), Quaternion.identity);
            Instantiate(edge, new Vector3(i, -9, 0), Quaternion.identity);
        }
        //y轴
        for (int i = -9; i <= 9; i++)
        {
            Instantiate(edge, new Vector3(10, i, 0), Quaternion.identity);
            Instantiate(edge, new Vector3(-10, i, 0), Quaternion.identity);
        }
    }

    // Update is called once per frame
    void Update()
    {
    }

    private void FixedUpdate()
    {

        PlayerMove();
    }

    // 坦克移动方法
    private void PlayerMove()
    {
        //按WSAD 或者方向键执行移动
        //垂直上下移动
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * moveSleep * Time.fixedDeltaTime, Space.World);
        if (v != 0)
        {
            //禁止坦克斜着走，优先上下移动
            return;
        }

        //水平左右移动
        float h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * h * moveSleep * Time.fixedDeltaTime, Space.World);

        if (createCD > 0)
        {
            createCD -= Time.fixedDeltaTime;
            return;
        }
        //添加墙
        if (Input.GetKey(KeyCode.F1))
        {
            createCD = 0.7f;
            Instantiate(Wall, transform.position, Quaternion.identity);
        }
        else if (Input.GetKey(KeyCode.F2))
        {
            createCD = 0.7f;
            Instantiate(Barriar, transform.position, Quaternion.identity);
        }
        else if (Input.GetKey(KeyCode.F3))
        {
            createCD = 0.7f;
            Instantiate(Grass, transform.position, Quaternion.identity);
        }
        else if (Input.GetKey(KeyCode.F4))
        {
            createCD = 0.7f;
            Instantiate(River, transform.position, Quaternion.identity);
        }
        else if (Input.GetKey(KeyCode.F5))
        {
            isClear = true;
        }
        else if (Input.GetKey(KeyCode.F6))
        {
            FindAll();
            createCD = 0.7f;
        }
    }

    private void OnTriggerEnter2D(Collider2D other) {
        Debug.Log("123");
        if(isClear){
            Debug.Log("123");
            isClear=false;
            if ("HeartTag".Equals(other.tag))
            {
                return;
            }
            //销毁对象
            Destroy(other.gameObject);
        }
    }

    private void FindAll()
    {
        GameObject[] all = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        foreach (GameObject child in all)
        {
            if("EdgeTag".Equals(child.tag)){
                continue;
            }
            Debug.Log(child.transform.name);
            Vector3 v3 = child.transform.position;
            Debug.Log("x:" + v3.x + "  y:" + v3.y + "  z:" + v3.z);
        }
    }
}
